import 'package:boss/color_constant.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';

class VerticalTabView extends StatefulWidget {
  final WidgetBuilder builder;
  final List<String> tabs;
  final int position;
  final tabHeight;
  final ValueChanged<int> onTabTap;
  final Widget footer;

  const VerticalTabView({
    @required this.builder,
    @required this.tabs,
    @required this.position,
    this.tabHeight,
    this.onTabTap,
    this.footer,
    Key key,
  }) : super(key: key);

  @override
  _VerticalTabViewState createState() => _VerticalTabViewState();
}

class _VerticalTabViewState extends State<VerticalTabView> {
  ScrollController _scrollController;

  @override
  void initState() {
    super.initState();

    _scrollController = ScrollController();
  }

  @override
  void dispose() {
    _scrollController.dispose();
    _scrollController = null;
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Container(
          width: 160.w,
          color: ColorConstant.f5f5,
          child: ListView.builder(
            controller: _scrollController,
            padding: EdgeInsets.zero,
            itemBuilder: (context, index) {
              if (widget.footer != null && index == widget.tabs.length) {
                return widget.footer;
              }

              final isSelected = index == widget.position;

              return VerticalTab(
                widget.tabs[index],
                isSelected: isSelected,
                onTap: () {
                  onTapHandle(index);
                },
              );
            },
            itemCount: widget.tabs.length + (widget.footer == null ? 0 : 1),
            itemExtent: widget.tabHeight,
          ),
        ),
        Expanded(child: widget.builder(context))
      ],
    );
  }

  void onTapHandle(int index) {
    _scrollIndex2Center(index);
    if (widget.onTabTap != null) widget.onTabTap(index);
  }

  _scrollIndex2Center(int index) {
    var offset = _scrollCenterIndexOffset(index);

    _scrollController.animateTo(
      offset,
      duration: Duration(milliseconds: 300),
      curve: Curves.decelerate,
    );
  }

  double _scrollCenterIndexOffset(int index) {
    var maxScrollExtent = _scrollController.position.maxScrollExtent;
    var minScrollExtent = _scrollController.position.minScrollExtent;
    var viewportDimension = _scrollController.position.viewportDimension;

    var itemHeight = 90.w;
    var centerOffsetTop = (itemHeight * index + itemHeight * (index + 1)) / 2;

    return (centerOffsetTop - viewportDimension / 2.0).clamp(minScrollExtent, maxScrollExtent);
  }
}

class VerticalTab extends StatelessWidget {
  final String text;
  final VoidCallback onTap;
  final TextStyle textStyle;
  final bool isSelected;

  const VerticalTab(
    this.text, {
    this.isSelected = false,
    this.onTap,
    this.textStyle,
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      behavior: HitTestBehavior.translucent,
      child: Container(
        color: isSelected ? Colors.white : null,
        alignment: Alignment.center,
        child: Text(
          text,
          style: textStyle ?? (isSelected ? TextStyleConstant().normal_1_32 : TextStyleConstant().normal_3_28),
        ),
      ),
    );
  }
}
